package com.tedu.bank_ems1.controller;

import com.tedu.bank_ems1.entity.Dept;
import com.tedu.bank_ems1.service.impl.DeptServiceImpl;

import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：毕龙
 * Date: 2024/6/25 15:02
 */
@WebServlet(value = "/dept/all")
public class DeptListServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置响应的乱码解决
        resp.setCharacterEncoding("utf-8");

        List<Dept> list = new DeptServiceImpl().listDept();
        //使用JSONObject对象将集合转化为Json串
        JSONObject object = new JSONObject();
        object.put("depts",new JSONArray(list));
        PrintWriter writer = resp.getWriter();
        writer.print(object.toString());

    }
}
